using System;
using System.Collections.Generic;
using System.Linq;
using MyApp.WebApi.Entity;
using MyApp.WebApi.ParamModel;
using MyApp.WebApi.Repository;
using MyApp.WebApi.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;


namespace Admin3000.Backend.Controllers
{
    //评论类型列表
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class TypesController : ControllerBase
    {
        private IRepository<Types> _typesRespository;

        private TokenParameter _tokenParameter;

        private readonly IConfiguration _configuration;

        public TypesController(
            IConfiguration configuration,
          IRepository<Types> typesRespository

        )
        {
            _configuration = configuration;
            _typesRespository = typesRespository;
            _tokenParameter =
                _configuration
                    .GetSection("TokenParameter")
                    .Get<TokenParameter>();
        }


        //获取评论类型列表
        [HttpGet, Route("getlist")]
        public dynamic GetList()
        {


            var list = _typesRespository.Table.ToList();
            return list;
        }

        //评论类型表分页
        [HttpGet]
        public dynamic Get([FromQuery] Pager pager)
        {
            var pageIndex = pager.PageIndex;
            var pageSize = pager.PageSize;
            var rowsTotal = pager.RowsTotal;


            // 获得所有满足条件的记录
            var users =
                _typesRespository.Table.Where(x => x.IsDeleted == false);

            //  var users = from program in _typesRespository.Table
            //             join articleprogram in _articleRespository.Table on program.Id equals articleprogram.ProgramId
            //             orderby articleprogram.Id
            //             select new
            //             {
            //                 Id = program.Id,
            //                IsActived = program.IsActived,
            //                 IsDeleted = program.IsDeleted,
            //                 CreatedTime = program.CreatedTime,
            //                 Remarks = program.Remarks

            //             };

            // 对获得的记录分页
            var u =
                users.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            // 返回的数据里，带有数据和分页信息
            var res =
                new
                {
                    Code = 1000,
                    Data =
                        new
                        {
                            Data = u,
                            Pager =
                                new
                                {
                                    pageIndex,
                                    pageSize,
                                    rowsTotal = users.Count()
                                }
                        },
                    Msg = "获取评论类型列表成功^_^"
                };
            return res;
        }
        //获取评论类型表指定Id信息
        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var AritpId = _typesRespository.GetById(id);

            if (AritpId == null)
            {
                return new { Code = 1000, Data = "", Msg = "指定评论类型不存在" };
            }
            return new
            {
                Code = 1000,
                Data = AritpId,
                Msg = "获取评论类型列表成功^_^"
            };
        }

        // 评论类型表添加
        [HttpPost]
        public string Post(Types model)
        {
         
           var commentType=model.CommentType.Trim();
                  
            if (
                string.IsNullOrEmpty(commentType)

            )
            {
                var tmp =
                    new
                    {
                        Code = 1004,
                        Data = "",
                        Msg = "评论类型不能为空，请确认后重试"
                    };
                return JsonHelper.Serialize(tmp);
            }

            var user =
                new Types
                {
                    CommentType = model.CommentType
                 

                };

            _typesRespository.Insert(user);

            var res = new { Code = 1000, Data = user, Msg = "添加评论类型成功" };
            return JsonHelper.Serialize(res);
        }
        // 评论类型表修改指定id
        [HttpPut("{id}")]
        public dynamic Put(int id, Types model)
        {
            var TypesId = _typesRespository.GetById(id);

            if (TypesId != null)
            {
                TypesId.CommentType = model.CommentType;
               
                // user.Password = model.Password;
                TypesId.UpdatedTime = DateTime.Now;
                _typesRespository.Update(TypesId);
                return JsonHelper
                    .Serialize(new
                    {
                        Code = 1000,
                        Data = TypesId,
                        Msg =
                            string
                                .Format("你修改评论类型的id为：{0}，已经修改成功，请注意查收",
                                id)
                    });
            }
            else
            {
                return JsonHelper
                    .Serialize(new
                    {
                        Code = 104,
                        Data = "",
                        Msg = "指定Id的评论类型不存在，请确认后重试"
                    });
            }
        }
        // 评论类型表删除
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            var AritpId = _typesRespository.GetById(id);
            if (AritpId != null)
            {
                _typesRespository.Delete(id);
                return new
                {
                    Code = 1000,
                    Msg = string.Format("删除指定id为{0}的评论类型成功", id)
                };
            }
            else
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "指定Id的评论类型不存在，请确认后重试"
                };
            }
        }
    }
}
